package com.lst.servlet;

import java.io.PrintWriter;
import java.util.Date;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.alibaba.druid.util.StringUtils;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.lst.base.entity.BaseRequest;
import com.lst.base.entity.BaseResponse;
import com.lst.common.CommCode;
import com.lst.entity.request.AnswerReqPara;
import com.lst.model.Answer;
import com.lst.model.Question;
import com.lst.service.AnswerService;
import com.lst.service.QuestionService;
import com.lst.utils.DateUtil;
import com.lst.utils.RequestUtils;


/**
 * @ClassName: AnswerDetailServlet
 * @Description: 回答删除接口
 * 
 */
public class AnswerDeleteServlet extends BaseServlet {

	/**
	 * @Fields serialVersionUID
	 */
	private static final long serialVersionUID = 1L;

	private AnswerService answerService = ctx.getBean(AnswerService.class);
	
	private QuestionService questionService = ctx.getBean(QuestionService.class);
	
	@Override
	public void doGet(HttpServletRequest request, HttpServletResponse response) {
		doPost(request, response);
	}

	@Override
	public void doPost(HttpServletRequest request, HttpServletResponse response) {
		Date startDate = new Date();
	//	logger.info("doPost begin: " + startDate);
		BaseResponse resPara = new BaseResponse();

		try {
			AnswerReqPara reqPara = getReqPara(request);
		
		if(isCheckField(reqPara,resPara)){
//			Integer userid = Integer.valueOf(reqPara.getUserid());
			Integer answerId = Integer.valueOf(reqPara.getAnswerId());	
			Integer answerSourceId = Integer.valueOf(reqPara.getAnswerSourceId());	
			
			Answer answer=answerService.selectByPrimaryKey(answerId);
			if(answer!=null){
				answerService.delete(answerId);
				
				Question question=questionService.selectByPrimaryKey(answerSourceId);
				int answerNum=question.getAnswerNum();
				question.setAnswerNum(answerNum-1);
				questionService.updateQuestion(question);
				
				resPara.setCode(CommCode.M_Y000000);
				resPara.setMessage(CommCode.M_Y000001);
			}else{
				resPara.setCode(CommCode.M_A000000);
				resPara.setMessage("该回答已经删除");
			}
			
		  }
		} catch (Exception e) {
			resPara.setCode(CommCode.M_A000000);
			resPara.setMessage(CommCode.M_SYS0001);

			logger.error("doPost error: ", e);
		}
		
		try {
			response.setContentType("application/json;charset=utf-8");
			response.setHeader("pragma", "no-cache");
			response.setHeader("cache-control", "no-cache");

			Gson gson = new GsonBuilder().setDateFormat("yyyy-MM-dd HH:mm:ss").excludeFieldsWithoutExposeAnnotation().serializeNulls().create();
			String jsonResult = gson.toJson(resPara);
	//		logger.info("AnswerDeleteServlet jsonResult:" + jsonResult);

			PrintWriter out = response.getWriter();
			out.print(jsonResult);
			out.flush();
			out.close();

		//	logger.info("doPost end run(s): " + DateUtil.calLastedTime(startDate));
		} catch (Exception e) {
			resPara.setCode(CommCode.M_A000000);
			resPara.setMessage(CommCode.M_SYS0003);

			logger.error("doPost error: ", e);
		}
	}

	@Override
	public AnswerReqPara getReqPara(HttpServletRequest request) {

		AnswerReqPara reqPara = null;

		try {
			BaseRequest baseRequest = RequestUtils.getRequestPara(request, new AnswerReqPara());

			reqPara = (AnswerReqPara) baseRequest.clone();

			String userid = request.getParameter("userid");
			String answerId = request.getParameter("answerId");
			String answerSourceId = request.getParameter("answerSourceId");
			
	//		logger.info("reqPara userid:" + userid);
	//		logger.info("reqPara answerId: " + answerId);
	//		logger.info("reqPara answerSourceId: " + answerSourceId);
		
            reqPara.setAnswerSourceId(answerSourceId);
	        reqPara.setAnswerId(answerId);
	        reqPara.setUserid(userid);

		} catch (Exception e) {
			logger.error("getReqPara error: ", e);
		}

		return reqPara;
	}

	@Override
	public boolean isCheckField(BaseRequest reqPara, BaseResponse resPara) {
		boolean flag = false;

		AnswerReqPara cdReqPara = (AnswerReqPara) reqPara;
		BaseResponse cdResPara = (BaseResponse) resPara;

		if (StringUtils.isEmpty(cdReqPara.getAnswerId())) {
			cdResPara.setCode(CommCode.M_A000000);
			cdResPara.setMessage("回答id为空");
		} else if (StringUtils.isEmpty(cdReqPara.getAnswerSourceId())) {
			cdResPara.setCode(CommCode.M_A000000);
			cdResPara.setMessage("回答目标id为空");
		}else if (StringUtils.isEmpty(cdReqPara.getUserid())) {
			cdResPara.setCode(CommCode.M_A000000);
			cdResPara.setMessage(CommCode.M_P000002);
		} else {
			flag = true;
		}

		return  flag;
	}

}
